package com.hmall.gateway.fliters;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * Description: 自定义全局过滤器
 *
 * @Author MaYuHan
 */
@Component
public class MyGlobalFilters implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // TODO 模拟登录校验逻辑
        HttpHeaders headers = exchange.getRequest().getHeaders();
        System.out.println("heards=" + headers);
        return chain.filter(exchange);
    }

    /**
     * 获取该对象的顺序值。
     * 值越高优先级越低。因此，
     * 具有最低值的对象具有最高优先级(在某种程度上)
     * 类似于Servlet  load-on-startup值。
     * 相同的顺序值将导致任意的排序位置
     * 受影响的对象。
     *
     * @return the order value
     * @see #HIGHEST_PRECEDENCE
     * @see #LOWEST_PRECEDENCE
     */
    @Override
    public int getOrder() {
        // 过滤器执行顺序，值越小，优先级越高
        return 0;
    }
}
